import 'package:chop/Model/address_entity.dart';
import 'package:chop/Model/ajax_request_entity.dart';
import 'package:chop/common/DioUtils.dart';
import 'package:chop/common/SPUtils.dart';
import 'package:chop/common/StringUtils.dart';
import 'package:chop/view/map/New_location.dart';
import 'package:chop/widgets/TextSizeWidget.dart';
import 'package:chop/widgets/Textwidget.dart';
import 'package:chop/widgets/TextwidgetNewaddress.dart';
import 'package:chop/widgets/ToastWidget.dart';
import 'package:chop/widgets/appBar.dart';
import 'package:chop/widgets/loadingDialog.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';


class New_address extends StatefulWidget {
  final String sOrT;

  const New_address({Key key, this.sOrT}) : super(key: key);
  @override
  _New_addressState createState() => _New_addressState();
}

class _New_addressState extends State<New_address> {
  TextEditingController _address; //地址
  TextEditingController _Specific_floor_number; //具体门牌号
  TextEditingController _telephone_number; //发货人电话
  TextEditingController _Specific_name; //发货人姓名
  AdrEntityData data;
  String sOrT;
  @override
  void initState() {
    // TODO: implement initState
    this.sOrT = widget.sOrT;
    super.initState();
    _address = TextEditingController();
    _Specific_floor_number = TextEditingController();
    _telephone_number = TextEditingController();
    _Specific_name = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: BaseViewBar(
          childView: BaseTitleBar("请添加您的常用地址"),
          preferredSize: Size.fromHeight(50.0),
        ),
        body: Stack(
          children: <Widget>[
            ListView(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(20.0, 30.0, 10.0, 20.0),
                  child: Form(
//          key: _formKey,//绑定状态属性
                    child: Column(
                      children: <Widget>[
                        TextWidgetNew(
                          controller: _address,
                          helperText: "请在地图上选取送达地址",
                          labelText: "address",
                          click: () async {
                            data = await Get.to(NewLocation());

                            if(data != null){
                              print(data.address);
                              data.address = getAddress(data.address);
                              print(data.address);
                              _address.text = data.address;
                              print("____________<${data.address}");
                            }
                          },

                        ),
                        TextWidget(
                          controller: _Specific_floor_number,
                          labelText: "Specific floor number",
                          helperText: "请填写详细门牌号",
                        ),
                        TextWidget(
                          controller: _telephone_number,
                          labelText: "Specific telephone number",
                          helperText: "请填写您的电话",
                        ),
                        TextWidget(
                          controller: _Specific_name,
                          labelText: "Specific name",
                          helperText: "请填写您的姓名",
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            Positioned(
              bottom: 0,
              width: MediaQuery.of(context).size.width,
              child: Container(
                child: Row(
                  children: <Widget>[
                    Expanded(
//                      flex: 7,
                      child: RaisedButton(
                        //提交按钮
                        color: Colors.lightBlueAccent,
                        highlightColor: Colors.blueAccent,
                        splashColor: Colors.blue,
                        colorBrightness: Brightness.dark,
//                        shape: RoundedRectangleBorder(
//                          //四角圆角
//                            borderRadius: BorderRadius.circular(10)),
                        onPressed: () async {
                          if (StringUtils.isNull(_address.text)) {
                            return Toast.toast(context,
                                msg: "请填写收货地址",
                                position: ToastPostion.bottom);
                          } else if (StringUtils.isNull(
                              _telephone_number.text)) {
                            return Toast.toast(context,
                                msg: "请填写手机号码",
                                position: ToastPostion.bottom);
                          } else if (StringUtils.isNull(
                              _Specific_name.text)) {
                            return Toast.toast(context,
                                msg: "请填写收货人姓名",
                                position: ToastPostion.bottom);
                          } else {
                            showDialog<Null>(
                              context: context, //BuildContext对象
                              barrierDismissible: false,
                              builder: (BuildContext context) {
                                return LoadingDialog(
                                  //调用对话框
                                  text: 'Loding...',
                                );
                              },
                            );
                            print(_Specific_name.text.trim());
                            data.remark = _Specific_floor_number.text.trim();
                            data.phone = _telephone_number.text.trim();
                            data.name = _Specific_name.text.trim();
                            data.userId = await SPUtils.get("UID");
                            data.sOrT = sOrT;
                            DioUtils.addAddress(data).then((AjaxRequest result){
                              if(result.code==0){
                                Toast.toast(context,position: ToastPostion.bottom,msg: "添加成功");
                                Get.back();
                                Get.back(result: 1);
                              }
                            });
                          }
                        },
                        child: FixedSizeText(
                          '提交地址',
                          style: TextStyle(color: Colors.white, fontSize: 15),
                        ),
                      ),
                    ),

                  ],
                ),

              ),
            ),
          ],
        ));
  }
  
  
  String getAddress(String str){
    List<String> arr = str.split(",");
    return "${arr[0]} ${arr[1]}";
  }
}
